package com.cysd.ewmxns.sdw_ui.customview

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.view.menu.MenuBuilder
import androidx.core.content.ContextCompat
import qrrspeeelild.rqrc0ode.andro0ild.R

class VerticalBottomNavigationView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var menu: Menu? = null
    private var selectedItemId = 0
    private var listener: OnNavigationItemSelectedListener? = null
    private val itemViews = mutableListOf<View>()

    init {
        // 初始化菜单
        val menuResId = obtainMenuResource(attrs)
        if (menuResId != 0) {
            menu = MenuBuilder(context).apply {
                MenuInflater(context).inflate(menuResId, this)
            }
            createMenuItems()
        }
    }

    private fun obtainMenuResource(attrs: AttributeSet?): Int {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalBottomNavigationView)
        val menuRes = typedArray.getResourceId(R.styleable.VerticalBottomNavigationView_menu, 0)
        typedArray.recycle()
        return menuRes
    }

    private fun createMenuItems() {
        removeAllViews()
        itemViews.clear()

        val linearLayout = LinearLayout(context).apply {
            layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
            orientation = LinearLayout.HORIZONTAL
            weightSum = menu?.size()?.toFloat() ?: 0f
        }

        menu?.let { menu ->
            for (i in 0 until menu.size()) {
                val menuItem = menu.getItem(i)
                val itemView = createItemView(menuItem)
                itemViews.add(itemView)
                linearLayout.addView(itemView)
            }
        }

        addView(linearLayout)
    }

    private fun createItemView(menuItem: MenuItem): View {
        val inflater = LayoutInflater.from(context)
        val itemView = inflater.inflate(R.layout.bottom_nav_item, this, false)

        val icon = itemView.findViewById<ImageView>(R.id.nav_icon)
        val label = itemView.findViewById<TextView>(R.id.nav_label)
        val container = itemView.findViewById<View>(R.id.nav_item_container)

        // 设置图标和文本
        icon.setImageDrawable(menuItem.icon)
        label.text = menuItem.title

        // 设置点击事件
        container.setOnClickListener {
            setSelectedItem(menuItem.itemId)
            listener?.onNavigationItemSelected(menuItem) ?: true
        }

        // 初始化选中状态
        if (menuItem.itemId == selectedItemId) {
            setItemSelected(itemView, true)
        } else {
            setItemSelected(itemView, false)
        }

        return itemView
    }

    private fun setItemSelected(itemView: View, selected: Boolean) {
        val icon = itemView.findViewById<ImageView>(R.id.nav_icon)
        val label = itemView.findViewById<TextView>(R.id.nav_label)

        // 这里你可以设置选中状态的颜色变化
        val colorStateList = if (selected) {
            ColorStateList.valueOf(ContextCompat.getColor(context, R.color.tabselect))
        } else {
            ColorStateList.valueOf(ContextCompat.getColor(context, R.color.gray))
        }

        icon.imageTintList = colorStateList
        label.setTextColor(colorStateList)
    }

    fun setSelectedItem(itemId: Int) {
        selectedItemId = itemId
        updateSelection()
    }

    private fun updateSelection() {
        menu?.let { menu ->
            for (i in 0 until menu.size()) {
                val menuItem = menu.getItem(i)
                val itemView = itemViews.getOrNull(i)
                itemView?.let {
                    setItemSelected(it, menuItem.itemId == selectedItemId)
                }
            }
        }
    }

    fun setOnNavigationItemSelectedListener(listener: OnNavigationItemSelectedListener) {
        this.listener = listener
    }

    @SuppressLint("RestrictedApi")
    fun setMenu(menuResId: Int) {
        menu = MenuBuilder(context).apply {
            MenuInflater(context).inflate(menuResId, this)
        }
        createMenuItems()
    }

    interface OnNavigationItemSelectedListener {
        fun onNavigationItemSelected(item: MenuItem): Boolean
    }
}